﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.OfficeArt
{
    internal class OfficeArtFRITContainer : OfficeArtRecord
    {
        private List<OfficeArtFRIT> rgfrit;

        public override RecType RecordType
        {
            get { return RecType.OfficeArtFRITContainer; }
        }

        protected override void ReadNoHeader(OfficeArtReadContext context)
        {
            Contract.Assert(rhrecVer == 0x0);
            Contract.Assert(rhrecType == (RecType) 0xF118);
            Contract.Assert(rhrecVer == 0x0);
            Contract.Assert(rhrecVer == 0x0);
            rgfrit = new List<OfficeArtFRIT>();
            for (int i = 0; i < rhrecInstance; i++)
            {
                var item = new OfficeArtFRIT();
                item.Read(context.Reader);
                rgfrit.Add(item);
            }
        }

        protected override byte Version
        {
            get { return 0x0; }
        }

        protected override int ByteLength
        {
            get { return Enumerable.Sum(rgfrit, item => item.FullByteLength); }
        }


        protected override void WriteNoHeader(OfficeArtWriteContext context)
        {
            foreach (OfficeArtFRIT item in rgfrit)
                item.Write(context.Writer);
        }

        protected override void PrepareHeader(ref OfficeArtRecordHeader rh)
        {
            rh.recInstance = (ushort) this.rgfrit.Count;
        }
    }
}